package mysql

import (
	"gorm.io/gorm"
)

// 用户表
type User struct {
	gorm.Model
	UserName string `gorm:"type:varchar(20);not null;"`
	Password string `gorm:"type:varchar(32);not null;"`
	Email    string `gorm:"type:varchar(20);not null;"`
}

// 订单表
type Order struct {
	gorm.Model
	UserId  int     `gorm:"type:int(5);not null;comment:用户id"`
	OrderSn string  `gorm:"type:varchar(36);not null;;comment:商品编号唯一;unique"`
	Status  int     `gorm:"type:tinyint;not null;default:1;comment:1-待支付 2-已支付 3-已取消"`
	PayType int     `gorm:"type:tinyint;not null;default:1;comment:1-微信 2-支付宝"`
	PayTime int     `gorm:"type:int(11);not null;comment:支付时间"`
	Total   float64 `gorm:"type:decimal(10,2);not null;comment:订单总金额"`
	Remark  string  `gorm:"type:varchar(30);not null;comment:订单备注"`
}

// 订单商品表
type Order_Goods struct {
	gorm.Model
	UserId   int     `gorm:"type:int(5);not null;comment:用户id"`
	GoodsId  int     `gorm:"type:int(5);not null;comment:商品id"`
	OrderId  int     `gorm:"type:int(5);not null;comment:订单id"`
	Name     string  `gorm:"type:varchar(20);not null;;comment:商品名称"`
	Price    float64 `gorm:"type:decimal(10,2);not null;comment:商品价格"`
	GoodsNum int     `gorm:"type:int(5);not null;;comment:商品数量"`
}

// 购物车表
type Cart struct {
	gorm.Model
	UserId   int     `gorm:"type:int(5);not null;comment:用户id"`
	GoodsId  int     `gorm:"type:int(5);not null;comment:商品id"`
	Quantity int     `gorm:"type:int(5);not null;comment:商品数量"`
	Price    float64 `gorm:"type:decimal(10,2);not null;comment:总价"`
	Status   int     `gorm:"type:int(1);not null;default:1;comment:1-已选中 2-未选中"`
}

// Goods 商品表
type Goods struct {
	gorm.Model
	Name  string  `gorm:"type:varchar(20);not null;"`
	Price float64 `gorm:"type:decimal(10,2);not null;"`
	Stock int     `gorm:"type:int(5);not null;comment:商品数量"`
}

// 详情
func (g *Goods) Show(goodsId int) error {
	return DB.Where("id = ?", goodsId).Limit(1).Find(&g).Error
}

// 添加订单
func (o *Order) Create() error {
	return DB.Create(&o).Error
}

// 添加订单商品表
func (og *Order_Goods) Create() error {
	return DB.Create(&og).Error
}

// 订单商品详情
func (og *Order_Goods) Show(userId int) error {
	return DB.Where("user_id = ?", userId).Limit(1).Find(&og).Error
}
